package net.dipper.tankwar.editor.ui;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.xml.parsers.ParserConfigurationException;

import org.xml.sax.SAXException;

import net.dipper.tankwar.core.config.ConfigManager;

public class ObjectPane extends JPanel implements ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public final int DEFAULT_WIDTH = 240;
	
	private ArrayList<ActionListener> listeners;
	
	private ObjectList backgroundList;
	private ObjectList blockList;
	private ObjectList towerList;
	private ObjectList unitList;
	
	public ObjectPane() throws ClassNotFoundException, InstantiationException, IllegalAccessException, ParserConfigurationException, SAXException, IOException{
		this.listeners = new ArrayList<ActionListener>();
		
		ConfigManager manager = ConfigManager.getInstance();
		
		this.backgroundList = new ObjectList("背景", manager.getBackgroundConfig());
		this.blockList = new ObjectList("障碍物", manager.getBlockConfig());
		this.towerList = new ObjectList("塔防", manager.getTowerConfig());
		this.unitList = new ObjectList("单位", manager.getUnitConfig());
		
		this.backgroundList.addActionListener(this);
		this.blockList.addActionListener(this);
		this.towerList.addActionListener(this);
		this.unitList.addActionListener(this);
		
		BoxLayout layout = new BoxLayout(this, BoxLayout.Y_AXIS);
		setLayout(layout);
		
		add(this.backgroundList);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(this.blockList);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(this.towerList);
		add(Box.createRigidArea(new Dimension(0, 10)));
		add(this.unitList);
		
		setSize(new Dimension(180, 600));
	}
	
	public void addActionListener(ActionListener listener){
		this.listeners.add(listener);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		for(ActionListener listener : listeners){
			listener.actionPerformed(e);
		}
	}
}
